package com.git.smp.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.core.PageRequest;
import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.RoleMapper;
import com.git.smp.entity.Role;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/role")
public class RoleController {

	Logger logger = LoggerFactory.getLogger(RoleController.class);

	@Autowired
	RoleMapper roleMapper;

	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/selectByPage")
	public ResponseMessage<PageInfo<Role>> selectByPage(@RequestBody PageRequest<Role> pageRequest) {
		ResponseMessage<PageInfo<Role>> responseMessage = new ResponseMessage<PageInfo<Role>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询成功！");

		Role role = pageRequest.getBody();

		PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		List<Role> listRole = roleMapper.select(role);
		if (listRole == null) {
			responseMessage.setCode("0001");
			
			responseMessage.setMessage("查询菜单信息失败！");
		}
		PageInfo<Role> pageInfo = new PageInfo<Role>(listRole, pageRequest.getPageSize());
		responseMessage.setExtend(pageInfo);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}

	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/addRole")
	public ResponseMessage addRole(@RequestBody Role role) {
		ResponseMessage<PageInfo<Role>> responseMessage = new ResponseMessage<PageInfo<Role>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户新增成功！");
		try {
			roleMapper.insertSelective(role);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户新增失败！");
		}

		return responseMessage;
	}
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/updateRole")
	public ResponseMessage updateRole(@RequestBody Role role) {
		ResponseMessage<PageInfo<Role>> responseMessage = new ResponseMessage<PageInfo<Role>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户修改成功！");
		try {
			roleMapper.updateByPrimaryKeySelective(role);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户修改失败！");
		}

		return responseMessage;
	}
	
	/**
	 * 
	 * @param pageRequest
	 * @return
	 */
	@RequestMapping("/deleteRole")
	public ResponseMessage deleteRole(@RequestBody Role role) {
		ResponseMessage<PageInfo<Role>> responseMessage = new ResponseMessage<PageInfo<Role>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("用户删除成功！");
		
		try {
			roleMapper.delete(role);
		} catch (Exception e) {
			logger.error("",e);
			responseMessage.setCode("0001");
			responseMessage.setMessage("用户删除失败！");
		}
		
		return responseMessage;
	}
	
	//获取所有角色信息
	@RequestMapping("/getRoles")
	public ResponseMessage<List<Role>> getRoles(){
		ResponseMessage<List<Role>> responseMessage = new ResponseMessage<List<Role>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("所有角色信息获取成功！");
		
		List<Role> listRole = roleMapper.getRoles();
		responseMessage.setExtend(listRole);
		return responseMessage;
		
	}
	
	//根据用户名获取对应的角色信息
	@RequestMapping("/getRoleByUserName")
	public ResponseMessage<List<Role>> getRoleByUserName(
			@RequestParam String userName){
		ResponseMessage<List<Role>> responseMessage = new ResponseMessage<List<Role>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("根据用户名获取对应的角色信息成功！");
		
		List<Role> listUserRoles = roleMapper.getRoleByUserName(userName);
		responseMessage.setExtend(listUserRoles);
		return responseMessage;
		
	}
	
	
	
}
